﻿#region Usings

using System.Windows;
using System.Windows.Media;

#endregion

namespace TreeView
{
    public static class ControlHelper
    {
        #region Class Methods

        public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement, string name) where T: FrameworkElement
        {
            var count = VisualTreeHelper.GetChildrenCount(parentElement);
            if(count == 0)
                return null;

            for(var i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parentElement, i);
                var element = child as T;
                if(element != null && element.Name == name)
                {
                    return element;
                }

                var result = FindFirstElementInVisualTree<T>(child, name);
                if(result != null)
                    return result;
            }
            return null;
        }

        #endregion
    }
}